package com.cql.common.utils;

import com.cql.common.constant.FoodCircleConstant;

import java.util.Random;

/**
 * @author cql
 */
public class FoodCircleUtils {

    /**
     * 生成随机用户名，数字和字母组成
     *
     * @param length
     * @return
     */
    public static String getStringRandom(int length) {

        String val = "";
        Random random = new Random();

        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }


    /**
     * 获取1到size之前的随机数
     *
     * @param size
     * @return
     */
    public static int randomIndex(int size) {
        return (int) (Math.random() * size);
    }


    /**
     * 根据经验获取登录信息
     *
     * @param exp
     * @return
     */
    public static int getLevel(Long exp) {
        if (exp <= FoodCircleConstant.LV1) {
            return 1;
        }
        if (exp <= FoodCircleConstant.LV2) {
            return 2;
        }
        if (exp <= FoodCircleConstant.LV3) {
            return 3;
        }
        if (exp <= FoodCircleConstant.LV4) {
            return 4;
        }
        return 5;
    }
}
